﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Dapper_Core.BLL.Impl;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Dapper_Core.Web.Controllers
{
    public class WelcomeController : Controller
    {
        public IActionResult Index()
        {
            #region 测试多个用户同时登录时日志中的IP是否准确
            //NlogUtility.WriteInfo(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "刷新Welcome页面", "", "DapperCore"); 
            #endregion
            return View();
        }

        public IActionResult GetUnderCheckingList(int rowsPerPage, int offsetRows)
        {
            try
            {
                if (rowsPerPage < 1 || offsetRows < 0)
                {
                    return Json(new { total = 0, rows = new List<V_WelcomeUnderCheckingList>() });
                }
                string pinyinCode = SessionUtility.GetBasicUserInfo().PinyinCode;
                Expression<Func<V_WelcomeUnderCheckingList, bool>> approvingExp = approving => approving.CheckPinyinCode == pinyinCode;
                V_WelcomeUnderCheckingListBusiness approvingBusiness = new V_WelcomeUnderCheckingListBusiness();
                List<V_WelcomeUnderCheckingList> list = approvingBusiness.GetPagelist((offsetRows / rowsPerPage) + 1, rowsPerPage, approvingExp, "CreateDate desc").ToList();
                int count = approvingBusiness.GetListCount(approvingExp);
                return Json(new { total = count, rows = list });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "GetUnderCheckingList执行失败", $"rowsPerPage={rowsPerPage}，offsetRows={offsetRows}", ex, "DapperCore");
                return Json(new { total = 0, rows = new List<V_WelcomeUnderCheckingList>() });
            }
        }

        public IActionResult GetAllSubmitList(int rowsPerPage, int offsetRows)
        {
            try
            {
                if (rowsPerPage < 1 || offsetRows < 0)
                {
                    return Json(new { total = 0, rows = new List<V_WelcomeAllSubmitList>() });
                }
                string pinyinCode = SessionUtility.GetBasicUserInfo().PinyinCode;
                Expression<Func<V_WelcomeAllSubmitList, bool>> approvingExp = approving => approving.ProxyMan == pinyinCode;
                V_WelcomeAllSubmitListBusiness approvingBusiness = new V_WelcomeAllSubmitListBusiness();
                List<V_WelcomeAllSubmitList> list = approvingBusiness.GetPagelist((offsetRows / rowsPerPage) + 1, rowsPerPage, approvingExp, "CreateDate desc").ToList();
                int count = approvingBusiness.GetListCount(approvingExp);
                return Json(new { total = count, rows = list });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "GetAllSubmitList执行失败", $"rowsPerPage={rowsPerPage}，offsetRows={offsetRows}", ex, "DapperCore");
                return Json(new { total = 0, rows = new List<V_WelcomeAllSubmitList>() });
            }
        }
    }
}